package com.MP_fianl;

import java.util.ArrayList;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class ImagePreference {
	 private ArrayList<Integer> _currentState;
	 private ArrayList<ImageView> mPreference;
	 private Integer[] _twoState = {R.drawable.emotion_unselected, R.drawable.emotion_selected};
	 private static final String TAG = "CameraTest";
	 private int _prefer_count;
	 private int _num;
		public ImagePreference( ArrayList<?> drawable, int num, boolean onclickable) {
			_num = num;
			_prefer_count = 0;
		    mPreference = new ArrayList<ImageView>();
		    _currentState = new ArrayList<Integer>();

		    for( int i = 0; i < num; ++i){
		    	mPreference.add(i, (ImageView) drawable.get(i));
		    	_currentState.add(i,0);
		    }
		    if(onclickable)
		    	init();
		}
		
		private void init()
		{
		    int i = 0;
			for(  i = 0; i < _num; i += 1){
				final int counter = i;
				Log.v(TAG, "final: "+counter);
				mPreference.get(counter).setOnClickListener(new ImageView.OnClickListener()
				{
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if(_currentState.get(counter) == 0){
							for( int j = 0; j <= counter; ++j){
								_currentState.set(j, 1);
								mPreference.get(j).setImageResource(_twoState[1]);
							}
							_prefer_count = counter+1;
						}
						else if(_currentState.get(counter) == 1)
						{
							for(int j = counter; j < _num; ++j){
								_currentState.set(j, 0);
								mPreference.get(j).setImageResource(_twoState[0]);
							}
							_prefer_count = counter;
						}
					}
					
				});
			}	
		}
		
		
		public void reset()
		{
			for(  int i = 0; i < _num; i++){
				_currentState.set(i, 0);
				mPreference.get(i).setImageResource(_twoState[_currentState.get(i)]);
			}
		}
		public int getTotalPreference()
		{
			return _prefer_count;
		}
		public void setTotalPreference(int total)
		{
			for( int i = 0; i < total ; ++i){
				mPreference.get(i).setImageResource(_twoState[1]);
			}
			for( int i = total; i < _num; ++i){
				mPreference.get(i).setImageResource(_twoState[0]);
			}
		}
}
		
		
